package joney.main.views.report;


import java.awt.Color;
import java.util.Vector;

import joney.main.analysis.DateComparison;
import joney.model.Account;
import joney.model.DataModel;
import joney.model.Expense;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;

public class MonthlyBreakdownView extends TimeRangeChart{

	public static String ID = "joney.montlybreakdownview";
	
	
	private void setMonthData(DefaultCategoryDataset dataset, int yearb, int yeare)
	{
		final String months[] = 
        {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

        for(int i=0; i < months.length; i++){
        
        Vector<Double> values = new Vector<Double>();
        Vector<String> accountNames = new Vector<String>();
        
        //cal values
        for(Account a: DataModel.accounts)
        {
        	accountNames.add(a.getName());
        	
        	double amount=0;
        	
        	for(Expense e: DateComparison.getAllExpenseFromMonth(a.getName(), i))
        	{
        		amount+=e.getAmount();
        	}
        	values.add(amount);
        }
        
        	for(int j=0; j < accountNames.size(); j++)
        		dataset.addValue(values.get(j),accountNames.get(j), months[i]);

        }
	}
	
	protected CategoryDataset createDataset(int yearb, int yeare) {
        
		// create the dataset...
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		
        setMonthData(dataset, yearb, yeare);
        	
        return dataset;
        
    }
	
	protected JFreeChart createChart(final Dataset dataset) {
	        
	        // create the chart...
	        final JFreeChart chart = ChartFactory.createBarChart(
	            "",         // chart title
	            "Month",               // domain axis label
	            "$",                  // range axis label
	            (CategoryDataset) dataset,                  // data
	            PlotOrientation.VERTICAL, // orientation
	            true,                     // include legend
	            true,                     // tooltips?
	            false                     // URLs?
	        );

	        // set the background color for the chart...
	        chart.setBackgroundPaint(Color.white);

	        // get a reference to the plot for further customisation...
	        final CategoryPlot plot = chart.getCategoryPlot();
	        plot.setBackgroundPaint(Color.lightGray);
	        plot.setDomainGridlinePaint(Color.white);
	        plot.setRangeGridlinePaint(Color.white);

	        // set the range axis to display integers only...
	        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

	        // disable bar outlines...
//	        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
//	        renderer.setDrawBarOutline(false);
//	        
//	        // set up gradient paints for series...
//	        final GradientPaint gp0 = new GradientPaint(
//	            0.0f, 0.0f, Color.blue, 
//	            0.0f, 0.0f, Color.lightGray
//	        );
//	        final GradientPaint gp1 = new GradientPaint(
//	            0.0f, 0.0f, Color.green, 
//	            0.0f, 0.0f, Color.lightGray
//	        );
//	        final GradientPaint gp2 = new GradientPaint(
//	            0.0f, 0.0f, Color.red, 
//	            0.0f, 0.0f, Color.lightGray
//	        );
//	        renderer.setSeriesPaint(0, Color.red);
//	        renderer.setSeriesPaint(1, gp1);
//	        renderer.setSeriesPaint(2, gp2);

	        final CategoryAxis domainAxis = plot.getDomainAxis();
	        domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
	        );
	        // OPTIONAL CUSTOMISATION COMPLETED.
	        
	        return chart;
	        
	    }


	protected void updateData(int yearb, int yeare) {
		DefaultCategoryDataset dds = (DefaultCategoryDataset)dataset;
		dds.clear();
		setMonthData(dds, yearb, yeare);
	}

	protected void updateData(String account, int yearb, int yeare) 
	{
	}

}
